﻿using Decoration.Api.Comm;
using Decoration.Comm;
using Decoration.IService;
using Decoration.Models;
using Decoration.Models.DTO;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserInfoController : ControllerBase
    {
        private readonly TokenHelper _tokenHelper;
        IUserInfoService svc;
        public UserInfoController(IUserInfoService svc, TokenHelper _tokenHelper)
        {
            this.svc = svc;
            this._tokenHelper = _tokenHelper;
        }
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var info = svc.Query(o => o.Id == id).FirstOrDefault();

            return new Result() { code = 1, msg = "成功", data = info };
        }

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        [HttpGet("getAll")]
        public Result GetAll()
        {
            var data = svc.Query(o=>true);

            return new Result() { code=1, msg="成功",data = data};
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="UserName">分页参数</param>
        /// <param name="PageSize">每页显示的页数</param>
        /// <param name="PageIndex">显示第几页</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Query")]

        public PagedResult<UserInfoDto> Query(string? UserName, int PageSize = 10, int PageIndex = 1)
        {
            var SQL = @"select ui.*,li.LoginName,li.LoginLastTime,li.LoginCount,li.ProfilePhoto
                        from Base_UserInfos ui
                        left join Base_LoginInfos li on ui.Id=li.UserId
                        where 1=1 ";
            if (!string.IsNullOrEmpty(UserName))
            {
                SQL += @" and ui.UserName like @UserName ";
            }
            //计算总条数
            var SQLCount = @"select Count(*) num from (" + SQL + ")a ";
            var total = svc.ExecuteScalar<int>(SQLCount, new { UserName = "%" + UserName + "%" });
            SQL += @"order by ui.Id desc
            offset  @PageSize*(@PageIndex-1)rows fetch next  @PageSize  rows only;";
            var list = svc.Query<UserInfoDto>(SQL, new { UserName = "%" + UserName + "%", PageSize, PageIndex }).ToList();
            return new PagedResult<UserInfoDto>() { code = 1, msg = "成功", total = total, data = list };
        }

        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post(UserLoginDto entity)
        {

            var info = new Base_UserInfo()
            {
                UserName = entity.UserName,
                Sex = entity.Sex,
                Tel = entity.Tel,
                Email = entity.Email.IsNullOrEmpty() ? "" : entity.Email,
                IdCard = entity.IdCard.IsNullOrEmpty() ? "" : entity.IdCard,
                DeptId = entity.DeptId,
                CreateTime = DateTime.Now,
                Status = 1
            };
            var logininfo = new Base_LoginInfo()
            {
                LoginName = entity.LoginName,
                LoginPwd = Tools.Md5(entity.LoginPwd, "X2"),
                LoginCount = 0,
                LoginLastTime = DateTime.Now,
                CreateTime = DateTime.Now,
                Status = 1,
                OpenId = "",
                ProfilePhoto = info.Sex=="男"? "male.png":"female.png"
            };
         
            info.Base_LoginInfos.Add(logininfo);
            var res = svc.Add(info);
            if (res)
            {
                return new Result() { code = 1, msg = "成功" };
            }
            else
            {
                return new Result() { code = -1, msg = "失败" };
            }
        }
        [HttpDelete]
        public Result Delete(int Id)
        {
            var data = svc.Get(o => o.Id == Id);
            if (data != null)
            {
                data.Status = 0;
                //逻辑删除
                //var entity = _mapper.Map<Base_UserInfo>(data);
                var res = svc.Update(data);
                //物理删除
                //bll.Delete(info);
                if (res)
                {
                    return new Result() { code = 1, msg = "成功" };
                }
                else
                {
                    return new Result() { code = -1, msg = "删除失败" };
                }
            }
            else
            {
                return new Result() { code = -1, msg = "你要删除的数据不存在" };
            }
        }
        [HttpPut]
        public Result Update(Base_UserInfo info)
        {
            var data = svc.Get(o => o.Id.Equals(info.Id));
            if (data == null)
            {
                return new Result() { code = -1, msg = "查无此人" };
            }
            data.UserName = info.UserName;
            data.Tel = info.Tel;
            data.Email = info.Email;
            data.IdCard = info.IdCard;
            data.Sex = info.Sex;
            data.DeptId = info.DeptId;
            data.Status = info.Status;
            var list = svc.Update(data);
            if (list != null)
            {
                return new Result() { code = 1, msg = "成功" };
            }
            else
            {
                return new Result() { code = -1, msg = "失败" };
            }
        }
    }
}
